require 'open-uri'
require 'rubygems'
require 'sequel'
require 'socket'
require 'rexml/document'
include REXML

content = File.new("../config/#{Socket.gethostname}.yml").read
settings = YAML::load content
DB = Sequel.connect("#{settings['adapter']}://#{settings['username']}:#{settings['password']}@#{settings['host']}/#{settings['database']}")
basic_info = DB[:basic_info]

File.open("../data/places.tsv", "r").readlines.each do |line|
  woeid = line.split(/\t/).first
  if basic_info[:woeid => woeid].nil?
    url = "http://weather.yahooapis.com/forecastrss?w=#{woeid}&u=c"
    puts "fetch from " + url

    doc = URI.parse(url).read.gsub(/[&]/, 'and')
    rss = Document.new(doc).root

    unless rss.elements["channel/yweather:location"].nil? then
      city = rss.elements["channel/yweather:location"].attributes["city"]
      lat = rss.elements["channel/item/geo:lat"].text.to_f
      long = rss.elements["channel/item/geo:long"].text.to_f
      basic_info.insert(:woeid => woeid, :city => city, :lat => lat, :long => long)
    end

    puts woeid, lat, long
  end
end
